/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"7596ECA7-C21D-453A-8888-ADF514C747C7"}
 */
var fEjeY = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"C6962D09-EA13-457C-AB95-3133431BDF29"}
 */
var fEjeX = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"E57B48D9-E279-4AE6-8898-8C2B18516F09"}
 */
var fTipoGrafico = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"3A3B6E57-5F38-475A-92FD-0DD7785986ED"}
 */
var fTitulo = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"C5CB4C97-8D9D-4E1D-8507-483D637CF870"}
 */
var fResultado = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"C10880ED-1F50-42E0-99A5-D4573EB886B3",variableType:4}
 */
var fAnio = null;

/**
 * @type {Array}
 * 
 * @properties={typeid:35,uuid:"777469A7-3101-4248-B3EC-AE67A828CA6A",variableType:-4}
 */
var fResultados = new Array();

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"91EE9DC0-9D1D-4C4C-BD87-9F6C9E79979F"}
 */
function buscar(event) {
	
	if (!globals.gEnsayo || !fAnio || !fResultados)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Por favor, complete todos los datos','OK')
		return
	}
	var $param = fResultados.map(function() {return "?"}).join(",")
	var query = 'SELECT COUNT (paciente_id) as "cant",  resultado, paciente_id '+
					'FROM analisis_dtl as "ad" '+
					'JOIN analisis_cab as "ac" ON ad.analisis_cab_id = ac.analisis_cab_id '+
					'JOIN muestra_destino as "md" ON md.muestra_destino_id = ac.muestra_destino_id '+
					'JOIN muestras as "m" ON m.muestra_id = md.muestra_id '+
					'WHERE ensayo_id = ? AND date_part(\'year\',m.fecha) = ? AND resultado IN ('+ $param+') '+
					'GROUP BY paciente_id, resultado '+
					'ORDER BY resultado;'
	var max 	= -1
	var args 	= [globals.gEnsayo, fAnio]
	args 		= args.concat(fResultados)
	var ds 		= databaseManager.getDataSetByQuery('cepario',query,args,max)
	
	if (ds.getMaxRowIndex() == 0)
	{
		globals.DIALOGS.showWarningDialog('Aviso','No se encontraron muestras con esos datos','OK')
		return
	}
	
	var query = 'SELECT paciente_id '+
					'FROM analisis_dtl as "ad" '+
					'JOIN analisis_cab as "ac" ON ad.analisis_cab_id = ac.analisis_cab_id '+
					'JOIN muestra_destino as "md" ON md.muestra_destino_id = ac.muestra_destino_id '+
					'JOIN muestras as "m" ON m.muestra_id = md.muestra_id '+
					'WHERE ensayo_id = ? AND date_part(\'year\',m.fecha) = ? AND resultado IN ('+ $param+') '+
					'GROUP BY paciente_id '
	var $ds_2 = databaseManager.getDataSetByQuery('cepario',query,args,max)
	
	var $cant_total = $ds_2.getMaxRowIndex()
	
	var $titulo = fTitulo
	while ($titulo.indexOf(' ') != -1)
	{
		$titulo = $titulo.replace(' ','+')
	}
	
	var $chm		= '&chm=N,bbe9ff,0,,12,,c|N,005782,1,,12,,c|N,005782,2,,12,,c|N,005782,3,,12,,c'
	var $url 		= 'https://chart.googleapis.com/chart?cht=bvs&chs=575x330&chxt=x,y,x,y&chbh=50&chtt='+$titulo+$chm
	var $colores 	= '005782,0091d7,359aff,bbe9ff'
	var $separador_ref = '|'
	
	var $ref 	= '&chxl=2:|'+fEjeX+'|3:|'+fEjeY+'|0:|'
	var $data 	= '&chd=t:'
	var $leg 	= '&chdl='

	var $cant 		= ds.getMaxRowIndex()
	var $totales	= new Array()
	var $total 		= 0
	var i 			= 1
	var $ref_val	= ''
	
	/** @type {String}*/
	var $res_ant 	= ds.getValue(1,2)
	while (i <= $cant)
	{
		ds.rowIndex = i
		if ($res_ant != ds[2])
		{
			$totales.push($total)
			$total 		= 0
			$ref_val	+= $res_ant + $separador_ref
			$res_ant 	= ds[2]
		}
		$total += ds[1]
		i++
	}
	$ref_val += $res_ant
	$totales.push($total)
	
	var $limite_1 = $totales.length
	var $separador = ','
	for (var j = 0 ; j < $limite_1 ; j++)
	{
		if (j != 0)
		{
			$data += $separador
		}
		var $dato = $totales[j]
		var $avg = $dato / $cant_total * 100
		$data 	+= $avg.toFixed(2)
	}
	$url += '&chco='+$colores + $data + $ref + $ref_val + $leg
	elements.lbl.imageURL = $url
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"5062CCB2-90C5-49F0-BEFE-04FC87497C0B"}
 */
function imprimir(event) {
	var $name = 'estadisticas_2_imp'
	history.removeForm($name)
	solutionModel.removeForm($name)
	
	var $frm = solutionModel.cloneForm($name,solutionModel.getForm('estadisticas_2'))
	$frm.styleClass = 'impresion'
	var $cmp = $frm.getComponents()
	for (var i = 0 ; i < $cmp.length ; i++)
	{
		$cmp[i].styleClass = 'impresion'
	}
	forms[$name].controller.recreateUI()
	forms[$name].elements.lbl.imageURL = elements.lbl.imageURL
	forms[$name].fTitulo 	= fTitulo
	forms[$name].fResultado = fResultado
	forms[$name].fAnio 		= fAnio
	forms[$name].controller.showPrintPreview(true)
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"DF291D68-659F-4B41-AE41-3B9F745C295F"}
 */
function iraResultados(event) {
	globals.formEnVentana('estadisticas_2_resultados','RESULTADOS','Elija los resultados')
}
